package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-03-23.
 *
 * @author zhoufy
 */
@Controller
@RequestMapping("/")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("")
    public String index(Integer page, Model model) {
        if(page == null || page < 0){
            page = 0;
        }
        int pageCount = blogService.pageCount();

        if(page > pageCount){
            page = pageCount;
        }
        List<Article> articles = blogService.getList(page);
        model.addAttribute("articles", articles);
        model.addAttribute("page" ,page);
        return "index";
    }


    @RequestMapping("/app")
    @ResponseBody
    public Object appIndex() {
        List<Article> articles = blogService.getList(0);
        return articles;
    }

}
